package com.cqtczh.qhd.driver.acticity.user;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import tools.L;

import javabeen.CarTypeBeen;

import httptools.HttpJsonTool;
import adapter.CarTypeListAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.google.gson.reflect.TypeToken;
import com.cqtczh.qhd.driver.R;
import com.cqtczh.qhd.driver.acticity.base.BaseActivity;
import com.cqtczh.qhd.driver.api.GagApi;

/** 
 * @author  作者 E-mail: 
 * @date 创建时间：2015-6-3 下午8:20:47 
 * @version 1.0 
 * @parameter  
 * @since  
 * @return  
 */
public class CarType extends BaseActivity {

	//工作编码是查询车队类型还是常驻地区，長跑线路
	/**
	 * 200、是车型 201、是常驻地区 202、是常跑线路
	 */
	private	int workID;
	//汽车类型
	List<CarTypeBeen> carTypeBeens;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_route_cartype_carhomeactivity);
		Ini();
	}
	private void Ini(){
		//IniDATA();
	
		TextView dervierchose=(TextView) findViewById(R.id.dervierchose);
		dervierchose.setVisibility(View.GONE);
		Intent intent=getIntent();
		workID=intent.getIntExtra("WORK_TYPE", -1);
		StartWork();
		TextView title=(TextView) findViewById(R.id.head_title);
		title.setVisibility(View.VISIBLE);
		title.setText("车辆类型");
		ImageView left=(ImageView) findViewById(R.id.head_left);
		left.setVisibility(View.VISIBLE);
		left.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				CarType.this.finish();
			}
		});
		Button are_upload=(Button) findViewById(R.id.are_upload);
		are_upload.setVisibility(View.GONE);
	}
	
	
	
	
	private void StartWork(){
		String url="";
		showLoadingDialog("加载中...");
		if (workID==200) {
			//http://192.168.1.109:8080/cqtczh/cartype/listJsonCartype.do
			url=GagApi.CAR_TYPE;
					//"http://192.168.1.109:8080/cqtczh/cartype/listJsonCartype.do";
		}
		HashMap< String, String> data=new HashMap<String, String>();
		HttpJsonTool httpJsonTool=new HttpJsonTool(this);
		httpJsonTool.SetOnLister(new Handler(){
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				dismissLoadingDialog();
				super.handleMessage(msg);
				if (msg.what==1) {
					carTypeBeens=(List<CarTypeBeen>) msg.obj;
					L.v("carTypeBeens", "carTypeBeens获得");
					IniListView();
				}
			}
		});
		
		Type type=new TypeToken<List<CarTypeBeen>>(){}.getType();
		httpJsonTool.StartWork(data,url , type, null);
	}
	
	private void IniListView(){
		ListView listView=(ListView) findViewById(R.id.listView1);
		CarTypeListAdapter carTypeListAdapter=new CarTypeListAdapter(this, carTypeBeens, R.layout.car_type_list_iteam);
		listView.setAdapter(carTypeListAdapter);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				CarTypeBeen carTypeBeen=carTypeBeens.get(arg2);
				Intent intent=new Intent(CarType.this,MessageActivity.class);
				intent.putExtra("car_type", carTypeBeen);
				CarType.this.setResult(1, intent);
				CarType.this.finish();
			}
			
			
		});
		
	}
	
	
}
